//减法
export const subtract = function (arg1, arg2) {
    let r1, r2, m, n;
    try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
    try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
    m = Math.pow(10, Math.max(r1, r2));
    n = (r1 >= r2) ? r1 : r2;
    return ((arg1 * m - arg2 * m) / m).toFixed(n);
}
// 减法Big
export const subtractBig = (a, b) => {
    try {
        return new Big(a).minus(new Big(b));
    } catch {
        return a - b;
    }
};
//加法
export const addition = function (num1, num2) {//要相加的两个数
    let baseNum, baseNum1, baseNum2;
    try {
        baseNum1 = num1.toString().split(".")[1].length;
    } catch (e) {
        baseNum1 = 0;
    }
    try {
        baseNum2 = num2.toString().split(".")[1].length;
    } catch (e) {
        baseNum2 = 0;
    }
    baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
    return (num1 * baseNum + num2 * baseNum) / baseNum;
};

// 加法Big
export const additionBig = (a, b) => {
    try {
        return new Big(a).plus(new Big(b)); // 使用 .plus 方法
    } catch {
        return a + b;
    }
};

//乘法
export const multiplication = function (arg1, arg2) {
    let m = 0, s1 = arg1.toString(), s2 = arg2.toString();
    try { m += s1.split(".")[1].length } catch (e) { }
    try { m += s2.split(".")[1].length } catch (e) { }
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
}

// 乘法Big
export const multiplicationBig = (a, b) => {
    try {
        return new Big(a).times(new Big(b));
    } catch {
        return a * b;
    }
};

// 乘法保留两位小数
export const multiplicationFixed = (a, b, c = 2) => {
    try {
        return new Big(a).times(new Big(b)).toFixed(c);
    }
    catch {
        return (Math.round(multiplication(a, b) * 1000) / 1000).toFixed(2)
    }
}

///除法
export const division = function (arg1, arg2) {
    let t1 = 0, t2 = 0, r1, r2;
    try { t1 = arg1.toString().split(".")[1].length } catch (e) { }
    try { t2 = arg2.toString().split(".")[1].length } catch (e) { }
    r1 = Number(arg1.toString().replace(".", ""))
    r2 = Number(arg2.toString().replace(".", ""))
    return multiplication((r1 / r2), Math.pow(10, t2 - t1));
}
// 除法Big
export const divisionBig = (a, b) => {
    try {
        return new Big(a).div(new Big(b))
    }
    catch {
        return a / b
    }
}
export const _ = {
    // 数字四舍五入到指定的精度
    // _.round(2.145,2)  // 2.15
    round(number, precision) {
        const func = Math['round']
        precision = precision == null ? 0 : (precision >= 0 ? Math.min(precision, 292) : Math.max(precision, -292))
        if (precision) {
            let pair = `${number}e`.split('e')
            const value = func(`${pair[0]}e${+pair[1] + precision}`)

            pair = `${value}e`.split('e')
            return +`${pair[0]}e${+pair[1] - precision}`
        }
    },
    baseSum(array, iteratee) {
        let result

        for (const value of array) {
            const current = iteratee(value)
            if (current !== undefined) {
                result = result === undefined ? current : (result + current)
            }
        }
        return result
    },
    // 数组求和
    // let arr = [1,2,3]
    // _.sum(arr) // 6
    sum(array) {
        return array != null && array.length ? this.baseSum(array, (value) => value) : 0;
    },
    // 判断一个对象是否包含指定的属性
    // const obj = { name: 'Tom', age: 18 };
    //  _.has(obj, 'age')  // true
    has(object, key) {
        const hasOwnProperty = Object.prototype.hasOwnProperty;
        return object != null && hasOwnProperty.call(object, key);
    },
    // 数组去重
    // let arr = [ 1 , 2 , 3 , '1']
    // _.mapSize(arr) // [ 1 , 2 , 3 ]
    mapSize(array) {
        return array != null && array.length ? [...new Set(array)] : array;
    },
    /**
     * 两个浮点数求和
     */
    FloatAdd(num1, num2) {
        let r1, r2, m;
        try {
            r1 = `${num1}`.split('.')[1].length;
        } catch (e) {
            r1 = 0;
        }
        try {
            r2 = `${num2}`.split('.')[1].length;
        } catch (e) {
            r2 = 0;
        }
        m = Math.pow(10, Math.max(r1, r2));
        return Math.round(num1 * m + num2 * m) / m;
    },
    /**
     * 两个浮点数相减
     */
    FloatSub(num1, num2) {
        let r1, r2, m, n;
        try {
            r1 = `${num1}`.split('.')[1].length;
        } catch (e) {
            r1 = 0;
        }
        try {
            r2 = `${num2}`.split('.')[1].length;
        } catch (e) {
            r2 = 0;
        }
        m = Math.pow(10, Math.max(r1, r2));
        n = r1 >= r2 ? r1 : r2;
        return this.toFixed(Math.round(num1 * m - num2 * m) / m, n);
    },
    /**
     * 浮点型数字运算精度丢失修复
     * @param {object} num 小数
     * @param {number} s 小数位数
     * @return {object} s位小数
     */
    toFixed(num, s) {
        var times = Math.pow(10, s);
        var des = num * times + 0.5;
        des = (parseInt(des, 10) / times) + '';

        var pos_decimal = des.indexOf('.');
        if (pos_decimal > 0) {
            while (des.length <= pos_decimal + s) {
                des += '0';
            }
        }
        return des;
    }
}